/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.subcomponents;

import com.elf.gwt.common.client.callback.SubComponentCallback;
import com.elf.gwt.common.client.subcomponent.CommonSubComponent;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.elf.gwt.store.client.GWTCommerceConstants;
import com.elf.gwt.store.client.StoreComponentController;
import com.elf.gwt.store.client.components.AccountComponent;
import com.elf.gwt.store.client.components.CartComponent;
import com.elf.gwt.store.client.components.ProductComponent;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  Subcomponent for logging into application
 */
public class AccountLoginSubComponent extends StoreSubComponent
{
	protected FormPanel form;
	public TextBox emailBox;
	public PasswordTextBox passwordBox;
	protected InlineHTML warningLabel;
	protected DialogBox dialogBox;
	
	/**
	 * Constructor
	 */
	public AccountLoginSubComponent(
			StoreComponentController controller, 
			SubComponentCallback callback)
	{
		super(controller, "AccountComponent", AccountComponent.LoginAccount, callback);
		if (accountComponent == null) accountComponent = (AccountComponent)controller.getComponent("AccountComponent");

		GWTCommerceConstants constants = controller.getStoreConstants();

		thisComponent.add(new InlineHTML("<br/>"));
		CommonUtils.addSubTitle(handle, thisComponent, constants.AccountComponentReturningAccount());
				
		Element element = Document.get().getElementById("loginForm");
		form = FormPanel.wrap(element, false);
		form.setVisible(true);
		thisComponent.add(form);

		Grid returningGrid = new Grid(1,1);
		returningGrid.setWidth("100%");
		returningGrid.setStyleName(handle + "SubPanel");
		returningGrid.getColumnFormatter().setStyleName(0, handle + "SubPanelCol0");
		thisComponent.add(returningGrid);
		
		InlineHTML forgottenPasswordLabel = new InlineHTML(getController().getStoreConstants().AccountComponentPasswordForgotten());
		forgottenPasswordLabel.setStyleName("ComponentLink");
		forgottenPasswordLabel.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				DialogBox dialogBox = getPasswordForgottenDialogBox();
				dialogBox.center();
				dialogBox.show();
			}
		});

		returningGrid.setWidget(0, 0, forgottenPasswordLabel);
		
		warningLabel = new InlineHTML();
		warningLabel.setStyleName("WarningLabel");
		warningLabel.addStyleName(handle + "WarningLabel");
		thisComponent.add(warningLabel);

		thisComponent.add(new InlineHTML("<br/>"));
		CommonUtils.addSubTitle(handle, thisComponent, constants.AccountComponentNewAccount());

		Grid newAccountGrid = new Grid(3,1);
		newAccountGrid.setStyleName(handle + "SubPanel");
		newAccountGrid.getColumnFormatter().setStyleName(0, handle + "SubPanelCol0");
		thisComponent.add(newAccountGrid);

		newAccountGrid.setHTML(0, 0, constants.AccountComponentNewAccountText());
		newAccountGrid.setHTML(1, 0, "&nbsp;");
		Button newButton = new Button(controller.getCommonConstants().Continue(),
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						setSuperAction(AccountComponent.CreateAccount);
						getCallback().onNavigate(thisComponent);
					}}
		);
		newAccountGrid.setWidget(2, 0, newButton);
	}

	/**
	 * build
	 */
	public void build() 
	{
		if (cartComponent == null) cartComponent = (CartComponent)controller.getComponent("CartComponent");
		if (productComponent == null) productComponent = (ProductComponent)controller.getComponent("ProductComponent");
		if (accountComponent == null) accountComponent = (AccountComponent)controller.getComponent("AccountComponent");
		GWTCommerceConstants constants = controller.getStoreConstants();
		int id = accountComponent.getAccountModel().getId();

		if (id > -1)
		{
			warningLabel.setHTML("");
		}
		else if (id == -5)
		{
			warningLabel.setHTML(constants.AccountComponentEmailAddressCheckErrorDomain() + "<br/>");
		}
		else if (id == -10)
		{
			warningLabel.setHTML(constants.AccountComponentBadUser() + "<br/>");
		}
		else if (id == -20)
		{
			warningLabel.setHTML(constants.AccountComponentBadPassword() + "<br/>");
		}
		else if (id == -30)
		{
			warningLabel.setHTML(constants.AccountComponentSessionExpired() + "<br/>");
			cartComponent.getCartModel().clearProducts();
		}
		else if (id == -100)
		{
			warningLabel.setHTML(constants.AccountComponentEmailAddressSuccess() + "<br/>");
		}
		else if (id == -120)
		{
			warningLabel.setHTML(constants.AccountComponentEmailAddressNotFound() + "<br/>");
		}
		else if (id == -130)
		{
			warningLabel.setHTML("Please close browser if leaving to remove login information available in history.<br/>");
		}
		else
		{
			warningLabel.setHTML(constants.AccountComponentUnknownError() + "<br/>");
		}
		
		if (id < 0) accountComponent.getAccountModel().update();
		
		Element formElement = form.getElement();
		NodeList<Element> inputs = formElement.getElementsByTagName("input");
		int ln = inputs.getLength();
		for (int i = 0; i < ln; i++)
		{
			Element element = inputs.getItem(i);
			InputElement inelement = (InputElement)element;
			String name = inelement.getName();
			if (name.equals("cart")) inelement.setValue(cartComponent.getCartModel().getCartContents());
			else if (name.equals("token")) inelement.setValue(String.valueOf(accountComponent.getToken()));
		}
				
	}

	/**
	 * 
	 * @param amodel
	 * @return
	 */
	protected DialogBox getPasswordForgottenDialogBox() {
		dialogBox = new DialogBox();
		dialogBox.setText("I've Forgotten My Password!");

		AccountPasswordForgottenSubComponent forgottenForm = new AccountPasswordForgottenSubComponent(
				controller,
				subComponentCallback, 
				"");

		dialogBox.setWidget(forgottenForm);
		dialogBox.setStyleName("AccountComponentDialogBox");
		// Return the dialog box
		return dialogBox;
	}

	/**
	 * 
	 */
	protected SubComponentCallback subComponentCallback = 
		new SubComponentCallback() {
		public void onNavigate(CommonSubComponent subComponent) {
			dialogBox.hide();
		}

		public void onSuccess(CommonSubComponent subComponent) {
			dialogBox.hide();
			setSuperAction(AccountComponent.PasswordForgotten);
			getCallback().onSuccess(subComponent);
		}

		public void onError(CommonSubComponent subComponent, Throwable exception) {}
	};
}
